package com.my.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.my.common.controller.BaseController;
import com.my.common.util.ConstatFinalUtil;
import com.my.common.util.PageUtil;
import com.my.users.pojo.AUser;
import com.my.users.service.IUserService;

/**
 *
 * @author mayue
 *
 */
@Controller
@RequestMapping("/back/user")
public class UserBackContorller extends BaseController
{

	@Resource
	private IUserService userService;

	/**
	 * 登陆后首页
	 * 
	 * @return
	 */
	@RequestMapping("/index.htm")
	public String index()
	{
		return "/back/index";
	}

	/**
	 * 
	 * @return
	 */
	@RequestMapping("/leftMenu.htm")
	public String leftMenu()
	{
		return "/back/leftMenu";
	}

	/**
	 * 退出
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/logOut.htm")
	public String logOut(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		/**
		 * 登录时放到session里的信息 remove
		 */
		session.removeAttribute("user");
		session.removeAttribute("lastLoginTime");
		return "redirect:/usersLogin.htm";
	}

	/**
	 * 管理员列表
	 * 
	 * @return
	 */
	@RequestMapping("/usersList.htm")
	public String usersList(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER_SYSTEM.info("--usersList--");
		PageUtil pageUtil = this.proccedPageUtil(request);
		/* 接收参数 */
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		if (st == null)
		{
			st = "";
		}
		if (ed == null)
		{
			ed = "";
		}

		Date stDate = null;
		Date edDate = null;
		if (!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = this.dateFormatUtil.parseDateTime(st);
			edDate = this.dateFormatUtil.parseDateTime(ed);
		}

		/* 查询数据 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("keyword", keyword);
		condMap.put("status", status);
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		List<AUser> usersList = this.userService.findListAUserService(pageUtil, condMap);
		/* 存储结果 */
		request.setAttribute("usersList", usersList);
		request.setAttribute("pageUtil", pageUtil);

		/* 存储条件 */
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		return "/back/usersList";
	}
	@RequestMapping("/userInsert.htm")
	public String userInsert()
	{
		return "/back/userInsert";
	}
	@RequestMapping(value ="/userInsertSubmit.htm",produces="text/html;charset=UTF-8")
	public @ResponseBody String userInsertSubmit(AUser user,HttpServletRequest request)
	{
		user.setPassword(this.encryptUtil.encryString(user.getPassword()));
		user.setCreateTime(new Date());
		user.setUpdateTime(new Date());
		user.setLastLoginTime(new Date());
		JSONObject resultJSON = this.userService.insertAUserService(user);
		resultJSON = this.returnJuiJSON(request, resultJSON.getString("info"));
		return resultJSON.toString();
	}
}
